module Utils where

readInt     = (read :: String -> Int)
readInteger = (read :: String -> Integer)
readDouble  = (read :: String -> Double)

groupn :: Int -> [a] -> [[a]]
groupn _ [] = []
groupn n xs = (take n xs) : (groupn n (drop n xs))

at :: Int -> [a] -> a
at n list = list !! n

tok :: Eq a => [a] -> [a] -> [[a]]
tok _ [] = []
tok delims xs = 
    token : tok delims rest
    where
        isDelimiter = (`elem` delims)
        token       = takeWhile (not . isDelimiter) xs
        rest        = dropWhile isDelimiter $ dropWhile (not . isDelimiter) xs

repl :: Eq a -> a -> a -> [a] -> [a]
repl _ _ [] = []
repl old new (x:xs) = 
    x' : (repl old new xs)
    where 
        x' = if (x /= old) then x else new

beginsWith :: Eq a => [a] -> [a] -> Bool
beginsWith [] _          = True
beginsWith _ []          = False
beginsWith (x:xs) (y:ys) = (x == y) && beginsWith xs ys

endsWith :: Eq a => [a] -> [a] -> Bool
endsWith x y = beginsWith (reverse x) (reverse y)